﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIZ;
using DAL;

namespace SinhNhat
{
    public partial class Service : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SERVICE service = (new ServiceBIZ()).GetByIDForUser(WebUtils.ToGuid(hdID.Value));

                if (service == null)
                {
                    Response.Redirect("/Index.aspx");
                }

                headerTitle.InnerText = service.Title;
                hdTitle.Value = service.Title;
                lblName.Text = service.Title;
                imgProduct.ImageUrl = service.Image;
                lrDescription.Text = service.Content;
                lblCreatedate.Text = service.CreateDate.Value.ToShortDateString();

                InitData();
            }
        }

        private void InitData()
        {
            List<PAYMENT> listPayment = new PaymentBIZ().GetAll();
            ddlPayment.DataSource = listPayment;
            ddlPayment.DataTextField = "Name";
            ddlPayment.DataValueField = "PaymentID";
            ddlPayment.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            SERVICEORDER serviceOrder = new SERVICEORDER();
            serviceOrder.ServiceOrderID = Guid.NewGuid();
            serviceOrder.CustomerName = txtName.Text;
            serviceOrder.CustomerEmail = txtEmail.Text;
            serviceOrder.CustomerAddress = txtAddress.Text;
            serviceOrder.CustomerPhone = txtPhone.Text;
            serviceOrder.Note = txtNote.Text;

            serviceOrder.PaymentID = WebUtils.ToGuid(ddlPayment.Value);
            serviceOrder.BankName = txtBankName.Text;
            serviceOrder.BankNumber = txtBankNumber.Text;

            serviceOrder.ServiceID = new Guid(hdID.Value);
            serviceOrder.Status = false;
            serviceOrder.CreateDate = DateTime.Now;

            ServiceOrderBIZ orderBIZ = new ServiceOrderBIZ();
            orderBIZ.Add(serviceOrder);

            //send email
            ParameterBIZ parameterBIZ = new ParameterBIZ();
            string from = parameterBIZ.GetByID(new Guid("C6360368-1BFF-4D41-93AE-48816B0FB179")).Value;
            string password = parameterBIZ.GetByID(new Guid("1754B1C3-404E-4B8C-87C8-5DFE4642BC02")).Value;
            string to = parameterBIZ.GetByID(new Guid("D9D71946-0265-45BC-A5A5-E2DDB5FDA3AE")).Value;

            string paymentName = new PaymentBIZ().GetByID(serviceOrder.PaymentID.Value).Name;
            string linkWeb = "http://" + Request.Url.Authority + "/admin/EditServiceOrder.aspx?serviceOrderID=" + serviceOrder.ServiceOrderID;
            string title = "Đơn đặt dịch vụ cuả " + serviceOrder.CustomerName;
            string body = string.Format("<h2>Thông tin khách hàng:</h2>Tên khách hàng:{0}<br/>Email:{1}<br/>Điện thoại:{2}<br/>Địa chỉ:{3}<br/>Ghi chú:{4}<br/>Hình thức thanh toán:{5}<h2>Tên dịch vụ:{6}</h2><br/><br/><br/>Xem chi tiết tại: {7}",
                serviceOrder.CustomerName, serviceOrder.CustomerEmail, serviceOrder.CustomerPhone, serviceOrder.CustomerAddress, serviceOrder.Note, paymentName, hdTitle.Value, linkWeb);
            WebUtils.sendMail(from, password, to, title, body);

            //success
            ClientScript.RegisterStartupScript(this.GetType(), "exampleScript", "<script language ='javascript'>alert('Đặt dịch vụ thành công. Chúng tôi sẽ sớm liên lạc với bạn.');CloseDialog();</script>");
        }
    }
}